package bigoo.common.communication;

public abstract class AbstractMessage implements Message {

	private String senderID;
	private String msgID;

	public AbstractMessage(String senderID, String msgID) {
		this.senderID = senderID;
		this.msgID = msgID;
	}

	@Override
	public final String getMessageIdentifier() {
		return this.msgID;
	}

	@Override
	public final String getSenderID() {
		return this.senderID;
	}


	public static String parseMessageID(String rawMessage) {
		String[] tokens = rawMessage.trim().split(SEPARATOR);
		return (tokens.length > 1) ? tokens[1]: rawMessage;
	}

	public static String parseSenderID(String rawMessage) {
		String[] tokens = rawMessage.trim().split(SEPARATOR);
		return tokens[0];
	}


	/**
	 * Returns a String Array that contains all params itens from params section.
	 * @param rawMessage the rawMessage
	 */
	public static String[] parseListParams(String rawMessage) {
		try {
			String[] tokens = rawMessage.trim().split(SEPARATOR);
			String[] params =  tokens[2].split(LIST_SEPARATOR);
			return params;
		} catch (ArrayIndexOutOfBoundsException e) {
			return new String[0];
		}
	}

	/**
	 * Parses a param token
	 * @param paramsToken
	 * @return
	 */
	public static String[] parseParams(String paramsToken) {
		return paramsToken.split(PARAM_SEPARATOR);
	}
}
